package com.qf.collection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection implements Collection {

    private Object[] arr = new Object[1000];

    private int count = 0;

    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count==0;
    }

    @Override
    public boolean contains(Object o) {

        for (int i = 0; i < count; i++) {
            if (arr[i].equals(o)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Iterator iterator() {

        // 匿名内部类的写法来实现特定的接口
        return new Iterator() {
            // 迭代器对象的起始下标
            private int index = 0;

            @Override
            public boolean hasNext() {
                return index<count;
            }

            @Override
            public Object next() {
                return arr[index++];
            }
        };

//        return new MyCollectionIterator(arr,count);
    }


    @Override
    public Object[] toArray() {
        Object[] objects = Arrays.copyOf(arr, count);
        return objects;
    }

    @Override
    public boolean add(Object o) {
        // o 到底存放到哪里呢 ？
        arr[count++] = o;
        return true;
    }

    @Override
    public boolean remove(Object o) {
        int index = -1;
        for (int i = 0; i < count; i++) {
            if (arr[i].equals(o)){
                index = i;
                break;
            }
        }
        if (index==-1){
            return false;
        }else {
            // 删除原数组中下标为 index的元素
            for (int i = index; i < count; i++) {
                arr[i] = arr[i+1];
            }
            count--; // 注意，要把count--
            return true;
        }
    }

    @Override
    public boolean addAll(Collection c) {

        for (Object o : c) {
            add(o);
        }

        return true;
    }

    @Override
    public void clear() {
        arr = new Object[1000];
        count = 0;
    }

    /**
     * 求集合的交集
     * @param c collection containing elements to be retained in this collection
     * @return
     */
    @Override
    public boolean retainAll(Collection c) {

        for (int i = 0; i < count; i++) {
            // arr[i] 是否在 集合c中
            if (c.contains(arr[i])){
                continue;
            }else {
                remove(arr[i]);
                i--;
            }
        }

        return true;
    }

    @Override
    public boolean removeAll(Collection c) {
        for (Object o : c) {
            remove(o);
        }

        return true;
    }

    @Override
    public boolean containsAll(Collection c) {
        for (Object o : c) {
            if (!contains(o)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Object[] toArray(Object[] a) {
        System.arraycopy(arr,0,a,0,count);
        return a;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer("[ ");
        for (int i = 0; i < count; i++) {
            stringBuffer.append(arr[i].toString());
            stringBuffer.append(" ");
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
}
